/**************************************************************************
 *
 * Copyright (C) 2007 University of California, Los Angeles.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *************************************************************************/

package edu.ucla.cs.typecast.bloomfilter;

import java.util.Arrays;
import java.util.Collection;

import edu.ucla.cs.typecast.util.TypeUtil;

/**
 *  This is a Utility class to encode a collection of types into a Bloom filter. 
 *
 */
public class BloomFilterEncoder {
	public static BloomFilter encode(Class[] types) { 
		return encode(Arrays.asList(types)); 
	}
	
	public static BloomFilter encode(Collection<Class> types) { 
		Collection<Class> allTypes = TypeUtil.getAllTypes(types); 
		BloomFilter filter = new BloomFilter(); 

		for(Class type: allTypes) { 
			int index = (int)(hash(type) % (BloomFilter.SIZE * 8)); 
			filter.setBit(index); 
		}		
		return filter; 				
	}
	
	private static long  hash(Class type) { 
		GeneralHashFunctionLibrary hashLibrary = new GeneralHashFunctionLibrary(); 
		return hashLibrary.ELFHash(type.getName()); 
	}
}
